This page last changed on Jan 11, 2009 by straha1.
Tutorial

This page is part of a tutorial that explains how to

Table of Contents

Simple Hello World

Let's try to compile this simple Fortran 90 program which we'll put in a file helloworld.f:

      write(*,*) "Salutations, Earth."
      end

To compile this using GCC and create the executable helloworld-f77-gcc, type

gfortran -ffixed-form helloworld.f -o helloworld-f77-gcc

Alternatively, to compile using PGI Fortran and create the executable helloworld-f77-pgi, type

pgf77 helloworld.f -o helloworld-f77-pgi

Running this program by typing ./helloworldf-77-gcc for GCC or ./helloworld-f77-pgi for PGI should produce this message:

 Salutations, Earth.

Parallel Hello World

Before you try to compile this program, you must set up your MPI and compiler using switcher as described here in the C tutorial. Make sure you follow the directions in that section before continuing.

Once you've used switcher to set up your compiler+MPI combination, create this Fortran 77 program in hello_parallel.f:

      program hello_parallel

C     Include the MPI library definitons:                                                                                
      include 'mpif.h'

      integer numtasks, rank, ierr, rc, len, i
      character*(MPI_MAX_PROCESSOR_NAME) name

C     Initialize the MPI library:                                                                                        
      call MPI_INIT(ierr)
      if (ierr .ne. MPI_SUCCESS) then
         print *,'Error starting MPI program. Terminating.'
         call MPI_ABORT(MPI_COMM_WORLD, rc, ierr)
      end if

C     Get the number of processors this job is using:                                                                    
      call MPI_COMM_SIZE(MPI_COMM_WORLD, numtasks, ierr)

C     Get the rank of the processor this thread is running on.  (Each                                                    
C     processor has a unique rank.)                                                                                      
      call MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierr)

C     Get the name of this processor (usually the hostname)                                                              
      call MPI_GET_PROCESSOR_NAME(name, len, ierr)
      if (ierr .ne. MPI_SUCCESS) then
         print *,'Error getting processor name. Terminating.'
         call MPI_ABORT(MPI_COMM_WORLD, rc, ierr)
      end if

      print 30, "hello_parallel.f: Number of",numtasks,rank,name
 30   format (A,' tasks=',I3,' My rank=',I3,' My name=',A80,'')
C     Tell the MPI library to release all resources it is using:                                                         
      call MPI_FINALIZE(ierr)

      end

Now that you've written your hello_parallel program, it is time to compile it with:

mpif77 hello_parallel.f -o hello_parallel

That same command will work with any of the six compiler+MPI combinations.

To learn how to run this program, continue on to this page.

If you intend to create more complex programs that use multiple source files, you will need to learn how to link programs. (For example, if you have one source file with your main program and three more files with subroutines needed by the main program.) To learn about linking MPI and non-MPI programs, go to this page.

Document generated by Confluence on Mar 31, 2011 15:37